﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AssetsSys.BLL.AutoMapper;
using AssetsSys.DAL;
using AssetsSys.Dto;
using AssetsSys.Dto.Department;
using AssetsSys.model;
using Department = AssetsSys.model.Department;

namespace AssetsSys.BLL
{
    /// <summary>
    /// 用户部门
    /// </summary>
    public class DepartmentManger
    {
        /// <summary>
        /// 获取部门列表
        /// </summary>
        /// <returns></returns>
        public static async Task<PageModel> GetList()
        {
            using (var depSvc = new DepartmentService())
            {
                var department = await depSvc.GetAll().OrderByDescending(x => x.CreateTime).Select(x=>new DepartmentModel()
                {
                    Id =x.Id,
                    DepartmentName = x.DepartmentName,
                    PermissionName = x.Permission.PermissionName,
                    PermissionMenu = x.Permission.PermissionMenu,
                }).ToListAsync();
                
                return new PageModel()
                {
                    Count = department.Count,
                    Data = department
                };
            }
        }

        /// <summary>
        /// 获取无管理部门列表
        /// </summary>
        /// <returns></returns>
        public static async Task<PageModel> GetNoAdminList(Guid managerId)
        {
            using (var depSvc = new DepartmentService())
            {
                var department = await depSvc.GetAll(x => x.Id != managerId).OrderByDescending(x => x.CreateTime).Select(x => new DepartmentModel()
                {
                    Id = x.Id,
                    DepartmentName = x.DepartmentName,
                    PermissionName = x.Permission.PermissionName,
                    PermissionMenu = x.Permission.PermissionMenu,
                }).ToListAsync();

                return new PageModel()
                {
                    Count = department.Count,
                    Data = department
                };
            }
        }

        /// <summary>
        /// 新增部门
        /// </summary>
        /// <param name="departmentName"></param>
        /// <param name="permissionId"></param>
        /// <param name="departmentId"></param>
        /// <returns></returns>
        public static async Task<bool> Create(string departmentName,Guid permissionId,Guid departmentId)
        {
            using (var depSvc = new DepartmentService())
            {
                await depSvc.CreateAsync(new Department()
                {
                    Id = departmentId,
                    DepartmentName = departmentName,
                    PermissionId = permissionId
                });
                return true;
            }
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static async Task<bool> Delete(Guid id)
        {
            using (var depSvc = new DepartmentService())
            {
                await depSvc.Remove(id);
                return true;
            }
        }

        /// <summary>
        /// 获取部门详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static async Task<DepartmentModel> GetById(Guid id)
        {
            using (var depSvc = new DepartmentService())
            {
                return (await depSvc.GetOne(id)).MapTo<DepartmentModel>();
            }
        }

        /// <summary>
        /// 更新部门
        /// </summary>
        /// <param name="departmentid"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public static async Task<bool> Update(DepartmentModel model)
        {
            using (var depSvc = new DepartmentService())
            {
                var oldModel = await depSvc.GetOne(model.Id);
                if (oldModel == null)
                {
                    return false;
                }
                oldModel.DepartmentName = model.DepartmentName ?? oldModel.DepartmentName;
                oldModel.PermissionId = model.PermissionId == Guid.Empty ? oldModel.PermissionId : model.PermissionId;
                await depSvc.EditAsvnc(oldModel);
            }
            return true;
        }
    }
}
